<template>
  <div class="app-container">
    <el-form ref="queryForm" size="small" :inline="true" label-width="68px">
        <el-row :gutter="10" class="mb8">
        <right-toolbar :search.sync="showSearch" @queryTable="getList"></right-toolbar>
        </el-row>

        <el-table v-loading="loading" :data="serverStatusList">
            <el-table-column label="代理名称" align="center" >
                <template slot-scope="scope">
                    {{frontProxytMap[Number(scope.row.proxyId)] == undefined ? scope.row.proxyId :frontProxytMap[Number(scope.row.proxyId)].proxyName}}
                </template>
            </el-table-column>
            <el-table-column label="绑定地址/端口" align="center" prop="feName" >
                <template slot-scope="scope">
                    {{frontProxytMap[Number(scope.row.proxyId)] == undefined ? "----:--" :frontProxytMap[Number(scope.row.proxyId)].bindIp + ":" + frontProxytMap[Number(scope.row.proxyId)].bindPort}}
                </template>
            </el-table-column>
            <el-table-column label="服务器名称" align="center" prop="srvId" >
                <template slot-scope="scope">
                    {{serverMap[Number(scope.row.srvId)] == undefined ? "--" :serverMap[Number(scope.row.srvId)].serverName}}
                </template>
            </el-table-column>
            <el-table-column label="服务器地址/端口" align="center" prop="srvAddr" >
                <template slot-scope="scope">
                    {{scope.row.srvAddr + ":" + scope.row.srvPort}}
                </template>
            </el-table-column>
            <el-table-column label="检测端口" width="80px" align="center" prop="srvCheckPort" />
            <el-table-column label="服务状态" width="100px" align="center">
                <template slot-scope="scope">
                    <dict-tag :options="dict.type.srv_op_state" :value="scope.row.srvOpState"/>
                </template>
            </el-table-column>
            <el-table-column label="管理状态" width="100px" align="center" prop="status">
                <template slot-scope="scope">
                    <dict-tag :options="dict.type.srv_admin_state" :value="scope.row.srvAdminState"/>
                </template>
            </el-table-column>


            <el-table-column label="操作" width="100" align="center" class-name="small-padding fixed-width">
              <template slot-scope="scope">
                <el-button
                  size="mini"
                  type="primary"
                  icon="el-icon-top"
                  v-if="scope.row.srvAdminState != 0"
                  @click="handleServerUp(scope.row)"
                >运维上线</el-button>
                <el-button
                  size="mini"
                  type="warning"
                  icon="el-icon-bottom"
                  v-if="scope.row.srvAdminState == 0"
                  @click="handleServerDown(scope.row)"
                >运维下线</el-button>
                
              </template>
              
            </el-table-column>

        </el-table>
    </el-form>
  </div>
</template>
<script>
import { listServerStatus, getFrontProxySummaryMap, getServerSummaryMap, serverDown, serverUp } from "@/api/lbmonitor/serverStatus";

export default {
  name: "ServerStatus",
  dicts: ['srv_op_state', 'srv_admin_state'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 唯一标识符
      uniqueId: "",
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: false,
      // 总条数
      total: 0,
      // 服务器状态表格数据
      serverStatusList: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        proxyName: null,
        mode: null,
      },
      // 表单参数
      form: {},
      //前端代理Map
      frontProxytMap: {},
      //后端服务器概要信息
      serverMap: {},
    };
  },
  created() {
  },
  activated() {
    getFrontProxySummaryMap().then(response => {
      this.frontProxytMap = response.data;
    });
    getServerSummaryMap().then(response => {
      this.serverMap = response.data;
    });
    this.getList();
    
  },
  methods: {
    /** 查询服务器状态列表 */
    getList() {
      this.loading = true;
      listServerStatus().then(response => {
        // console.log(response);
        this.serverStatusList = response.data.rows;
        // this.total = response.total;
        this.loading = false;
      });
    },
    handleServerUp(row){
      this.$modal.confirm('是否确认上线此服务器？').then(function() {
        return serverUp(row.proxyId, row.srvId);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("上线请求已提交，请刷新核对");
      }).catch(() => {
      });
      
    },
    handleServerDown(row){
      this.$modal.confirm('是否确认下线此服务器？').then(function() {
        return serverDown(row.proxyId, row.srvId);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("下线请求已提交，请刷新核对");
      }).catch(() => {
      });

    }
  }
};
</script>
